package com.demo.download.sample

import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.chad.library.adapter.base.BaseQuickAdapter
import com.demo.download.R
import com.demo.download.core.AppDownload
import com.demo.download.core.DownloadInfo
import com.demo.download.sample.glide.GlideApp

class AppHomeAdapter(
    layoutResId: Int = R.layout.item_app_home,
    data: MutableList<App>?,
    val lifecycleOwner: LifecycleOwner
) : BaseQuickAdapter<App, DownloadViewHolder>(layoutResId, data) {


    override fun convert(helper: DownloadViewHolder, item: App) {
        helper.tag = item.downloadUrl!!
        val ivIcon = helper.getView<ImageView>(R.id.iv_icon)
        GlideApp.with(ivIcon).load(item.icon).into(ivIcon)
        helper.setText(R.id.tv_name, item.name)
        val downloadScope = AppDownload.request(url = item.downloadUrl, data = item)
        downloadScope?.observer(lifecycleOwner, DownloadObserver(helper.tag, helper))
        val tbStatus = helper.getView<TextView>(R.id.tb_status)
        tbStatus.text = "下载"
        tbStatus.setTextColor(Color.argb(0xFF, 0x11, 0xB0, 0x77))
        val downloadInfo = downloadScope?.downloadInfo()
        val progressBar = helper.getView<ProgressBar>(R.id.progress_bar)
        progressBar.visibility = if (downloadInfo == null) View.GONE else View.VISIBLE
        downloadInfo?.let {
            val progressPercent =
                (it.currentLength.toFloat() / it.contentLength * 100).toInt()
            progressBar.progress = progressPercent
            when (it.status) {
                DownloadInfo.LOADING -> {
                    tbStatus.text = String.format("%s%s", progressPercent, "%")
                    progressBar.visibility = View.VISIBLE
                    tbStatus.setTextColor(Color.argb(0xFF, 0x11, 0xB0, 0x77))
                    tbStatus.setBackgroundResource(R.drawable.bt_background_normal)
                }
                DownloadInfo.PAUSE -> {
                    tbStatus.text = "继续"
                    progressBar.visibility = View.VISIBLE
                    tbStatus.setTextColor(Color.argb(0xFF, 0x11, 0xB0, 0x77))
                    tbStatus.setBackgroundResource(R.drawable.bt_background_normal)
                }
                DownloadInfo.WAITING -> {
                    tbStatus.text = "等待"
                    progressBar.visibility = View.VISIBLE
                    tbStatus.setTextColor(Color.argb(0xFF, 0x11, 0xB0, 0x77))
                    tbStatus.setBackgroundResource(R.drawable.bt_background_normal)
                }
                DownloadInfo.ERROR -> {
                    tbStatus.text = "重试"
                    progressBar.visibility = View.GONE
                    tbStatus.setTextColor(Color.RED)
                    tbStatus.setBackgroundResource(R.drawable.bt_background_error)
                }
                DownloadInfo.DONE -> {
                    tbStatus.text = "完成"
                    tbStatus.setTextColor(Color.DKGRAY)
                    progressBar.visibility = View.GONE
                    tbStatus.setBackgroundResource(R.drawable.bt_background_done)
                }
                else -> {
                    tbStatus.text = "下载"
                    progressBar.visibility = View.GONE
                    tbStatus.setTextColor(Color.argb(0xFF, 0x11, 0xB0, 0x77))
                    tbStatus.setBackgroundResource(R.drawable.bt_background_normal)
                }
            }
        }
    }

    inner class DownloadObserver(private val tag: Any, private val holder: DownloadViewHolder) :
        Observer<DownloadInfo> {

        override fun onChanged(t: DownloadInfo?) {
            if (tag == holder.tag) {
                try {
                    notifyItemChanged(holder.adapterPosition)
                } catch (e: Exception) {
                }
            }
        }
    }
}
